 package me.dan.alibabasdk.trade;

import java.util.Map;

import com.qyg.shop.core.alihandler.trade.logistics.AlibabaOpDeliverySendOrder;
import com.qyg.shop.core.alihandler.trade.logistics.AlibabaOpQueryLogisticCompanyList;
import com.qyg.shop.core.alihandler.trade.logistics.ParamsOfLogisticsSendOrderOnSellerView;

import me.dan.alibabasdk.client.DefaultAuthorizationTokenRepository;
import me.dan.alibabasdk.entity.ResponseEntity;
import me.dan.alibabasdk.entity.client.AuthorizationToken;
import me.dan.alibabasdk.infrastructure.GenericAPIPlot;
import me.dan.alibabasdk.infrastructure.HttpMethod;
import me.dan.alibabasdk.infrastructure.Protocol;
import me.dan.alibabasdk.infrastructure.SessionType;
import me.dan.alibabasdk.infrastructure.UriSchema;
import me.dan.alibabasdk.util.RequestParamModelToMap;

public class LogisticsSellerViewAPIPlot extends GenericAPIPlot{

    /*
     * 
     */
    private void init() {
        this.getRequestPolicy().setRequestBodyCharset("UTF-8");
        this.getRequestPolicy().setHttpMethod(HttpMethod.POST);
        this.getRequestPolicy().setSessionType(SessionType.Product);
        this.getRequestPolicy().setUriSchema(UriSchema.https);
        this.getRequestPolicy().getApi().setApiRoot("openapi");
        this.getRequestPolicy().getApi().setApiNamespace("com.alibaba.logistics");
        this.getRequestPolicy().getApi().setApiVersion("1");
        this.getRequestPolicy().setRequestProtocol(Protocol.param2);
        this.getRequestPolicy().setValidateSignatureRequired(true);
        this.setTokenRepository(new DefaultAuthorizationTokenRepository());
    }

    public LogisticsSellerViewAPIPlot() {

        super();
        init();
    }

    /**
     * 物流发货-自己联系物流发货URL
     * @param token
     * @param param
     * @return
     * @author SRZ
     */
    public ResponseEntity<AlibabaOpDeliverySendOrder> logisticsDeliveryOnSellerViewRetrieve(AuthorizationToken token,
        ParamsOfLogisticsSendOrderOnSellerView param) {

        this.getRequestPolicy().setClientId(token.getClientId());
        this.getRequestPolicy().setClientSecret(token.getClientSecret());
        this.getRequestPolicy().setAccessTokenOrPreAuthCode(token.getAccess_token());
        this.getRequestPolicy().setValidateSignatureRequired(true);
        this.getRequestPolicy().getApi().setApiName("alibaba.logistics.OpDeliverySendOrder.offline");
        this.getRequestPolicy().setRequestProtocol(Protocol.param2);

        ResponseEntity<AlibabaOpDeliverySendOrder> result = null;

        Map<String, Object> mappedParam = RequestParamModelToMap.ConvertRequestParamModelToMap(param);
        this.setParams(mappedParam);
        this.setResultType(param.getResponseClass());
        try {
            result = this.getInvokeClient().request(this,
                    this.getInvokeClient().getSerializerProvider().getSerializers(),
                    this.getInvokeClient().getSerializerProvider().getDeSerializers());
        } catch (Exception e) {

            e.printStackTrace();
        }
        return result;
    }
    
    /**
     * 物流公司列表-自联物流URL
     * @param token
     * @return
     * @author SRZ
     */
    public ResponseEntity<AlibabaOpQueryLogisticCompanyList> logisticsCompanyListOnSellerViewRetrieve(AuthorizationToken token) {

        this.getRequestPolicy().setClientId(token.getClientId());
        this.getRequestPolicy().setClientSecret(token.getClientSecret());
        this.getRequestPolicy().setAccessTokenOrPreAuthCode(token.getAccess_token());
        this.getRequestPolicy().setValidateSignatureRequired(true);
        this.getRequestPolicy().getApi().setApiName("alibaba.logistics.OpQueryLogisticCompanyList.offline");
        this.getRequestPolicy().setRequestProtocol(Protocol.param2);

        ResponseEntity<AlibabaOpQueryLogisticCompanyList> result = null;


        try {
            result = this.getInvokeClient().request(this,
                    this.getInvokeClient().getSerializerProvider().getSerializers(),
                    this.getInvokeClient().getSerializerProvider().getDeSerializers());

        } catch (Exception e) {

            e.printStackTrace();
        }

        return result;
    }
}
